//
//  ShopCarModelView.swift
//  sss
//
//  Created by Mac on 2021/1/14.
//  Copyright © 2021 hrrMac. All rights reserved.
//

import UIKit

class ShopCarModelView: UIView {
    
    let tableView = UITableView()
    let shopCarBottomView = ShoppingCarBottomView()
    
    var dataArr: [ShopCarModel] = []
    
    static let identifierCell = "shopCell"
    static let identifierHeader = "shopHeader"
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        configUI()
    }
    
    func configUI(){
        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = 115
        tableView.register(ShoppingCarTableViewCell.self, forCellReuseIdentifier: Self.identifierCell)
        tableView.register(ShoppingCarHeaderView.self, forHeaderFooterViewReuseIdentifier: Self.identifierHeader)
        
        addSubview(tableView)
        addSubview(shopCarBottomView)
    
        refreshFrame()
        
        viewToOperation()
    }
    
    func viewToOperation(){
        shopCarBottomView.touchAllSelectEvent = { isAllSelect in
            
        }
    }
    
    /// 刷新视图布局
    func refreshFrame(){
        shopCarBottomView.translatesAutoresizingMaskIntoConstraints = false
        shopCarBottomView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
        shopCarBottomView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
        shopCarBottomView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
        shopCarBottomView.heightAnchor.constraint(equalToConstant: 42).isActive = true
        
        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.bottomAnchor.constraint(equalTo: shopCarBottomView.topAnchor).isActive = true
        tableView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
        tableView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
        tableView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    }
    
    
    override func layoutSubviews() {
        refreshFrame()
    }
    
}



extension ShopCarModelView:UITableViewDelegate,UITableViewDataSource{
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataArr.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArr[section].dataArr.count
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 42
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header: ShoppingCarHeaderView = tableView.dequeueReusableHeaderFooterView(withIdentifier: Self.identifierHeader) as! ShoppingCarHeaderView
        header.shopNameLab.text = "swift商店"
        let ShopCarModel = dataArr[section]
        header.touchSelectEvent = { [self] isSelect in
            ShopCarModel.isSelect = isSelect
        }
        return header
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: ShoppingCarTableViewCell = tableView.dequeueReusableCell(withIdentifier: Self.identifierCell, for: indexPath) as! ShoppingCarTableViewCell
        cell.goodsNameLab.text = "购物车商品名称购物车商品名称购物车商品名称购物车商品名称购物车商品名称购物车商品名称"
        cell.goodsSpecificationLab.text = "规格：商品规格商品规格商品规格商品规格商品规格商品规格"
        cell.priceLab.text = "¥999999.00"
        cell.goodsImgView.backgroundColor = UIColor.gray
        cell.operationView.setParamert(indexPath.row)
        return cell
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
}


// 选择的一些操作
extension ShopCarModelView{
    /// 选中单个section
    func selectOneSection(_ section: Int, isSelect: Bool){
        let model = dataArr[section]
        model.isSelect = isSelect
        tableView.reloadSections(IndexSet(integer: section), with: .fade)
        
    }
    
    
    func isAllItemSelect() -> Bool{
        for mode in dataArr {
            if !mode.isSelect {
                return false
            }
        }
        return true
    }
    
    
    /// 底部视图参数修改
    func bottomViewValueChange(){
        shopCarBottomView.selectBtn.isSelected = isAllItemSelect()
        for model in dataArr {
            if model.isSelect {
                
            }
        }
        
        
        
    }
    
    
}
